//
//  ViewController.m
//  TestProfile
//
//  Created by Jaunce on 15/1/4.
//  Copyright (c) 2015年 Jaunce. All rights reserved.
//

#import "ViewController.h"
#import "UIView+Frame.h"
#import "ViewController1.h"
#import "ViewController2.h"
#import "UserHeaderView.h"

@interface ViewController ()

@property (weak, nonatomic) UITableView *tableView1;
@property (weak, nonatomic) UITableView *tableView2;

@property (weak, nonatomic) UserHeaderView *heaerView;

@end

@implementation ViewController

- (UserHeaderView *)heaerView
{
    if (_heaerView == nil) {
        _heaerView =  [[[NSBundle mainBundle] loadNibNamed:@"UserHeaderView" owner:self options:nil] objectAtIndex:0];
        [_heaerView.segmentControl addTarget:self action:@selector(onSegment:) forControlEvents:UIControlEventValueChanged];
        _heaerView.top = -_heaerView.height;
    }
    return _heaerView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    ViewController1 *vc1 = [[ViewController1 alloc] initWithNibName:nil bundle:nil];
    [self addChildViewController:vc1];
    [self.view addSubview:vc1.view];
    [vc1 didMoveToParentViewController:self];
    
    self.tableView1 = (UITableView *)vc1.view;
    self.tableView1.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
    
    ViewController2 *vc2 = [[ViewController2 alloc] initWithNibName:nil bundle:nil];
    [self addChildViewController:vc2];
    [self.view addSubview:vc2.view];
    [vc2 didMoveToParentViewController:self];
    
    self.tableView2 = (UITableView *)vc2.view;
    self.tableView2.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
    
    [self switchToContentViewWithIndex:0];
    
}

- (IBAction)onSegment:(UISegmentedControl *)sender {
    [self switchToContentViewWithIndex:sender.selectedSegmentIndex];
}

- (void)switchToContentViewWithIndex:(NSInteger)index
{
    //不同contentView的contentOffset会不一样，切换的时候会有跳闪的感觉，统一清零
    self.tableView1.contentOffset = CGPointMake(0, -self.tableView1.contentInset.top);
    self.tableView2.contentOffset = CGPointMake(0, -self.tableView2.contentInset.top);
    
    switch (index) {
        case 0:
            self.tableView1.hidden = NO;
            self.tableView2.hidden = YES;
            [self.tableView1 addSubview:self.heaerView];
            break;
        case 1:
            self.tableView2.hidden = NO;
            self.tableView1.hidden = YES;
            [self.tableView2 addSubview:self.heaerView];
            break;
        default:
            break;
    }
}

@end
